﻿namespace System.Globalization
{
    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.IO;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Threading;

    public class LanguageCollector
    {
        private ArrayList m_avalableCutureInfos;

        public LanguageCollector()
        {
            this.m_avalableCutureInfos = this.GetApplicationAvailableCultures();
            Debug.Assert(this.m_avalableCutureInfos != null);
        }

        public LanguageCollector(CultureInfo defaultCultureInfo) : this()
        {
            if (!this.m_avalableCutureInfos.Contains(defaultCultureInfo))
            {
                this.m_avalableCutureInfos.Add(defaultCultureInfo);
                this.m_avalableCutureInfos.Sort(new CultureInfoComparer());
            }
            Debug.Assert(this.m_avalableCutureInfos != null);
        }

        private Hashtable GetAllCultures()
        {
            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
            Hashtable hashtable = new Hashtable(cultures.Length);
            foreach (CultureInfo info in cultures)
            {
                hashtable.Add(info.Name, info);
            }
            return hashtable;
        }

        private ArrayList GetApplicationAvailableCultures()
        {
            ArrayList list = new ArrayList();
            Hashtable allCultures = this.GetAllCultures();
            string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            foreach (string str2 in Directory.GetDirectories(directoryName))
            {
                string fileName = Path.GetFileName(str2);
                CultureInfo info = (CultureInfo) allCultures[fileName];
                if (info != null)
                {
                    list.Add(info);
                }
            }
            return list;
        }

        private string GetDisplayName(CultureInfo cultureInfo, LanguageNameDisplay languageNameToDisplay)
        {
            switch (languageNameToDisplay)
            {
                case LanguageNameDisplay.DisplayName:
                    return cultureInfo.DisplayName;

                case LanguageNameDisplay.EnglishName:
                    return cultureInfo.EnglishName;

                case LanguageNameDisplay.NativeName:
                    return cultureInfo.NativeName;
            }
            Debug.Assert(false, string.Format("Not supported LanguageNameDisplay value {0}", languageNameToDisplay));
            return "";
        }

        public CultureInfoDisplayItem[] GetLanguages(LanguageNameDisplay languageNameToDisplay, out int currentLanguage)
        {
            CultureInfoDisplayItem[] itemArray = new CultureInfoDisplayItem[this.m_avalableCutureInfos.Count];
            currentLanguage = -1;
            string name = Thread.CurrentThread.CurrentUICulture.Name;
            string str2 = Thread.CurrentThread.CurrentUICulture.Parent.Name;
            for (int i = 0; i < this.m_avalableCutureInfos.Count; i++)
            {
                CultureInfo cultureInfo = (CultureInfo) this.m_avalableCutureInfos[i];
                string displayName = this.GetDisplayName(cultureInfo, languageNameToDisplay);
                itemArray[i] = new CultureInfoDisplayItem(displayName, cultureInfo);
                if ((name == cultureInfo.Name) || ((currentLanguage == -1) && (str2 == cultureInfo.Name)))
                {
                    currentLanguage = i;
                }
            }
            Debug.Assert((currentLanguage > -1) && (currentLanguage < this.m_avalableCutureInfos.Count));
            return itemArray;
        }

        private class CultureInfoComparer : IComparer
        {
            public int Compare(CultureInfo cix, CultureInfo ciy)
            {
                return string.Compare(cix.Name, ciy.Name);
            }

            int IComparer.Compare(object x, object y)
            {
                return this.Compare((CultureInfo) x, (CultureInfo) y);
            }
        }

        public enum LanguageNameDisplay
        {
            DisplayName,
            EnglishName,
            NativeName
        }
    }
}

